package com.jinlejia.erp.ui.new_house.adapter;


import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.DistanceUtil;
import com.bumptech.glide.Glide;
import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.ui.new_house.house.AddtionListActivity;
import com.jinlejia.erp.utils.SerializableObject;
import com.jinlejia.erp.utils.SharePTool;
import com.jinlejia.erp.utils.StringUtil;
import com.jinlejia.erp.widget.TagCloudView;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * 楼盘适配器
 */
public class NewHouseAdapter extends YfListAdapter<Map<String, Object>> {

    private Context context;


    public NewHouseAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext().getApplicationContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_new_house_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        TextView txt = view.findViewById(R.id.id_tips);
        txt.setText("暂无数据");
        ImageView img = view.findViewById(R.id.id_placeholder);
//        img.setImageResource(R.mipmap.ic_no_appeal);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, int i) {

        if (!(mData.get(i).get("imageUrl") + "ZP").equals("nullZP"))
            Glide.with(context).load(ApiConfig.MAGEHEADER_URL() + mData.get(i).get("imageUrl"))
                    .placeholder(R.mipmap.ic_no_img)
                    .error(R.mipmap.ic_no_img)
                    .into(((ViewHolder) viewHolder).houseImg);

//        ((ViewHolder) viewHolder).houseImg.setLabelVisual(false);

        ((ViewHolder) viewHolder).houseName.setText(StringUtil.valueOf(mData.get(i).get("houseName")));
        ((ViewHolder) viewHolder).houseArea.setText(StringUtil.valueOf(mData.get(i).get("county")));

        if (StringUtil.valueOf(mData.get(i).get("isSale")).equals("0") || StringUtil.valueOf(mData.get(i).get("isSale")).equals("0.0"))
            ((ViewHolder) viewHolder).housePrice.setText("价格待定");
        else
            ((ViewHolder) viewHolder).housePrice.setText(StringUtil.valueOf(mData.get(i).get("averagePrice_string")).concat("元/m²"));

        if (mData.get(i).containsKey("commissionType") && !TextUtils.isEmpty(StringUtil.valueOf(mData.get(i).get("commissionType")))) {
            if ((int) Double.parseDouble(StringUtil.valueOf(mData.get(i).get("commissionType"))) == 0) {
                ((ViewHolder) viewHolder).houseCommission.setText(StringUtil.valueOf(mData.get(i).get("commissionStr")));
            } else if ((int) Double.parseDouble(StringUtil.valueOf(mData.get(i).get("commissionType"))) == 1) {
                ((ViewHolder) viewHolder).houseCommission.setText(StringUtil.valueOf(mData.get(i).get("commissionStr")).concat("%"));
            } else if ((int) Double.parseDouble(StringUtil.valueOf(mData.get(i).get("commissionType"))) == 2) {
                ((ViewHolder) viewHolder).houseCommission.setText(StringUtil.valueOf(mData.get(i).get("commissionStr")).concat("元/套"));
            } else if ((int) Double.parseDouble(StringUtil.valueOf(mData.get(i).get("commissionType"))) == 3) {
                ((ViewHolder) viewHolder).houseCommission.setText(StringUtil.valueOf(mData.get(i).get("commissionStr")).concat("元/㎡"));
            }
        } else {
            ((ViewHolder) viewHolder).houseCommission.setText(StringUtil.valueOf(mData.get(i).get("commissionStr")));
        }
        //计算距离
        LatLng p1LL = new LatLng(Double.parseDouble(StringUtil.valueOf(mData.get(i).get("latitude"), "0")),
                Double.parseDouble(StringUtil.valueOf(mData.get(i).get("longitude"), "0")));
        LatLng p2LL = new LatLng(SharePTool.getLatitude(context), SharePTool.getLongitude(context));
//        LatLng p1LL = new LatLng(39.971802, 116.347927);
//        LatLng p2LL = new LatLng(39.892131, 116.498555);
        DecimalFormat df = new DecimalFormat("######0.00");
        ((ViewHolder) viewHolder).houseDistance.setText(df.format(DistanceUtil.getDistance(p1LL, p2LL) / 1000).concat("km"));
        List<String> sellList = new ArrayList<String>();
        if (mData.get(i).get("houseTagList") != null) {
            sellList= (List<String>)mData.get(i).get("houseTagList");
        }
        ((ViewHolder) viewHolder).houseSellPoint.setTags(sellList);
        ((ViewHolder) viewHolder).houseSellPoint.setFocusable(false);
        if (mData.get(i).containsKey("salesInfo") && mData.get(i).get("salesInfo") != null && !TextUtils.isEmpty(StringUtil.valueOf(mData.get(i).get("salesInfo")))) {
            //促销
            ((ViewHolder) viewHolder).promotion.setVisibility(View.VISIBLE);
            ((ViewHolder) viewHolder).promotion.setText(StringUtil.valueOf(mData.get(i).get("salesInfo")));
            ((ViewHolder) viewHolder).view.setVisibility(View.VISIBLE);
        } else {
            //没有促销
            ((ViewHolder) viewHolder).promotion.setVisibility(View.GONE);
            ((ViewHolder) viewHolder).view.setVisibility(View.GONE);
        }
        if (null != mData.get(i).get("buildingInfoList") && mData.get(i).containsKey("buildingInfoList") && StringUtil.valueOf(mData.get(i).get("buildingInfoList")).length() > 3) {
            ((ViewHolder) viewHolder).view.setVisibility(View.VISIBLE);
            ((ViewHolder) viewHolder).recommend.setVisibility(View.VISIBLE);
            ((ViewHolder) viewHolder).recommend.setText("详情查看加推");

            //--------------------解析加推列表--------------------------
            if (mData.get(i).containsKey("buildingInfoList") && !TextUtils.isEmpty(StringUtil.valueOf(mData.get(i).get("buildingInfoList")))) {
                final ArrayList<Map<String, Object>> addtionList = JSON.parseObject(StringUtil.valueOf(mData.get(i).get("buildingInfoList")),
                        new TypeReference<ArrayList<Map<String, Object>>>() {
                        });

                ((ViewHolder) viewHolder).recommend.setOnClickListener(v -> {
                    Intent addInttent = new Intent(context, AddtionListActivity.class);
                    SerializableObject smap = new SerializableObject();
                    smap.setObject(addtionList);
                    addInttent.putExtra("addtionList", smap);
                    addInttent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(addInttent);
                });
            }

        } else {
            //没有加推
            ((ViewHolder) viewHolder).recommend.setVisibility(View.GONE);
            ((ViewHolder) viewHolder).view.setVisibility(View.GONE);
        }
        viewHolder.itemView.setTag(mData.get(i));
    }


    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }


    private static final class ViewHolder extends RecyclerView.ViewHolder {
        //楼盘图片
        ImageView houseImg;
        //楼盘名
        TextView houseName;
        //楼盘所在区
        TextView houseArea;
        //楼盘距离
        TextView houseDistance;
        //楼盘类型
        TextView houseType;
        //价格
        TextView housePrice;
        //卖点
        TagCloudView houseSellPoint;
        //佣金
        TextView houseCommission;
        //促销
        TextView promotion;
        //推荐
        TextView recommend;
        View view;

        public ViewHolder(View itemView) {
            super(itemView);
            houseImg = itemView.findViewById(R.id.id_house_img);
            houseName = itemView.findViewById(R.id.id_house_name);
            houseArea = itemView.findViewById(R.id.id_house_area);
            houseDistance = itemView.findViewById(R.id.id_house_distance);
            houseType = itemView.findViewById(R.id.id_house_type);
            housePrice = itemView.findViewById(R.id.id_house_price);
            houseSellPoint = itemView.findViewById(R.id.id_house_sell_point);
            houseCommission = itemView.findViewById(R.id.id_commission);
            promotion = itemView.findViewById(R.id.id_promotion);
            recommend = itemView.findViewById(R.id.id_recommend);
            view = itemView.findViewById(R.id.center_line);
        }
    }


}
